<?php
namespace app\admin\controller;

class User extends Base{

    public function index(){
        $users=model('User')->getUsersByStatus(1);
        return $this->fetch('',[
            'users'=>$users,
            ]);
    }

    public function add(){

        if(request()->isPost()) {
            // 走插入逻辑
            $data = input('post.');
            if (!empty($data['id'])){
                return $this->update($data);
            }
            $validate = validate('User');
            if(!$validate->scene('add')->check($data)) {
                $this->error($validate->getError());
            }
            $username=model('User')->get(['username' =>$data['username']]);
            if($username&&$username!=$data['username']){
                $this->error('用户名已存在');
            }
            $email=model('User')->get(['email' =>$data['email']]);
            if($email&&$email!=$data['email']){
                $this->error('电子邮箱已存在');
            }
            $data['code'] = mt_rand(100, 10000);
            $userData = [
                'username' => $data['username'],
                'code' => $data['code'],
                'password' => md5($data['password'].$data['code']),
                'mobile' => $data['mobile'],
                'email' => $data['email'],
                'status' => 1,
            ];


            $adminId = model('User')->save($userData);
            if($adminId) {
                $this->success('添加成功', url('user/index'));
            }else {
                $this->error('添加失败');
            }

        }else {
            return $this->fetch('');
        }
    }

    public function edit($id){

        $user=model('User')->get(intval($id));
        return $this->fetch('',['user'=>$user]);
    }

    public function update($data){

        if(empty($data['password'])){
            $username=model('User')->get(['username' =>$data['username'],'id'=>['<>',intval($data['id'])]]);
            if($username&&$username!=$data['username']){
                $this->error('用户名已存在');
            }
            $email=model('User')->get(['email' =>$data['email'],'id'=>['<>',intval($data['id'])]]);
            if($email&&$email!=$data['email']){
                $this->error('电子邮箱已存在');
            }
            $adminData = [
                'username' => $data['username'],
                'mobile' => $data['mobile'],
                'email' => $data['email'],
                'status' => 1,
            ];
            $adminId = model('User')->save($adminData,['id' =>intval($data['id'])]);
            if($adminId) {
                $this->success('更新成功', url('user/index'));
            }else {
                $this->error('更新失败');
            }
        }else{
            $validate = validate('User');
            if(!$validate->scene('update')->check($data)) {
                $this->error($validate->getError());
            }
            $username=model('User')->get(['username' =>$data['username'],'id'=>['<>',intval($data['id'])]]);
            if($username&&$username!=$data['username']){
                $this->error('用户名已存在');
            }
            $email=model('User')->get(['email' =>$data['email'],'id'=>['<>',intval($data['id'])]]);
            if($email&&$email!=$data['email']){
                $this->error('电子邮箱已存在');
            }
            if ($data['password']!=$data['repassword']){
                $this->error('两次输入密码不一致');
            }
            $data['code'] = mt_rand(100, 10000);
            $userData = [
                'username' => $data['username'],
                'code' => $data['code'],
                'password' => md5($data['password'].$data['code']),
                'mobile' => $data['mobile'],
                'email' => $data['email'],
                'status' => 1,
            ];
            $adminId = model('User')->save($userData,['id' =>intval($data['id'])]);
            if($adminId) {
                $this->success('更新成功', url('user/index'));
            }else {
                $this->error('更新失败');
            }
        }
    }

    public function stoplist(){
        $users=model('User')->getUsersByStatus(0);
        return $this->fetch('',[
            'users'=>$users,
        ]);
    }

    public function delist(){
        $users=model('User')->getUsersByStatus(-1);
        return $this->fetch('',[
            'users'=>$users,
        ]);
    }



}